
<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Process extends MY_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('Process_model');
    }

    public function index() {
        $location = file_get_contents('http://freegeoip.net/json/122.176.78.38');
        //print_r($location);
        // echo $location;
        $ipdeatil=json_decode($location);
        $id = $_GET['id'];
        $this->Process_model->getUrl($id);
        $www = reset($this->Process_model->getUrl($id));
        $long_url = 'location:' . $www->long_url;
        $s = $this->getBrowser();
        $browser = $s['name'];
        $os = $this->os_info('');
        //    echo $browser . $os;
        $data['short_url'] = $id;
        $data['long_url'] = $www->long_url;
        $data['browser'] = $browser;
        $data['os'] = $os;
        $data['url_owner'] = $www->user_id;
        $data['ip'] = $ipdeatil->ip;
        // print_r($data);
        
        $row=$this->Process_model->checkIP($ipdeatil);
        if($row=='0'){        
        $this->Process_model->saveIP($ipdeatil);
        }
        $this->Process_model->saveUrl($data);
        header($long_url);
    }
    
    function getBrowser() {
        $u_agent = $_SERVER['HTTP_USER_AGENT'];
        $bname = 'Unknown';
        $platform = 'Unknown';
        $version = "";

        //First get the platform?
        if (preg_match('/linux/i', $u_agent)) {
            $platform = 'linux';
        } elseif (preg_match('/macintosh|mac os x/i', $u_agent)) {
            $platform = 'mac';
        } elseif (preg_match('/windows|win32/i', $u_agent)) {
            $platform = 'windows';
        }

        // Next get the name of the useragent yes seperately and for good reason
        if (preg_match('/MSIE/i', $u_agent) && !preg_match('/Opera/i', $u_agent)) {
            $bname = 'Internet Explorer';
            $ub = "MSIE";
        } elseif (preg_match('/Firefox/i', $u_agent)) {
            $bname = 'Mozilla Firefox';
            $ub = "Firefox";
        } elseif (preg_match('/OPR/i', $u_agent)) {
            $bname = 'Opera';
            $ub = "Opera";
        } elseif (preg_match('/Chrome/i', $u_agent)) {
            $bname = 'Google Chrome';
            $ub = "Chrome";
        } elseif (preg_match('/Safari/i', $u_agent)) {
            $bname = 'Apple Safari';
            $ub = "Safari";
        }  elseif (preg_match('/Netscape/i', $u_agent)) {
            $bname = 'Netscape';
            $ub = "Netscape";
        }

        // finally get the correct version number
        $known = array('Version', $ub, 'other');
        $pattern = '#(?<browser>' . join('|', $known) .
                ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
        if (!preg_match_all($pattern, $u_agent, $matches)) {
            // we have no matching number just continue
        }

        // see how many we have
        $i = count($matches['browser']);
        if ($i != 1) {
            //we will have two since we are not using 'other' argument yet
            //see if version is before or after the name
            if (strripos($u_agent, "Version") < strripos($u_agent, $ub)) {
                $version = $matches['version'][0];
            } else {
                $version = $matches['version'][1];
            }
        } else {
            $version = $matches['version'][0];
        }

        // check if we have a number
        if ($version == null || $version == "") {
            $version = "?";
        }

        return array(
            'userAgent' => $u_agent,
            'name' => $bname,
            'version' => $version,
            'platform' => $platform,
            'pattern' => $pattern
        );
    }

    function os_info($uagent) {
        // the order of this array is important
        global $uagent;
        $oses = array(
            'Win311' => 'Win16',
            'Win95' => '(Windows 95)|(Win95)|(Windows_95)',
            'WinME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',
            'Win98' => '(Windows 98)|(Win98)',
            'Win2000' => '(Windows NT 5.0)|(Windows 2000)',
            'WinXP' => '(Windows NT 5.1)|(Windows XP)',
            'WinServer2003' => '(Windows NT 5.2)',
            'WinVista' => '(Windows NT 6.0)',
            'Windows 7' => '(Windows NT 6.1)',
            'Windows 8' => '(Windows NT 6.2)',
            'WinNT' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
            'OpenBSD' => 'OpenBSD',
            'SunOS' => 'SunOS',
            'Ubuntu' => 'Ubuntu',
            'Android' => 'Android',
            'Linux' => '(Linux)|(X11)',
            'iPhone' => 'iPhone',
            'iPad' => 'iPad',
            'MacOS' => '(Mac_PowerPC)|(Macintosh)',
            'QNX' => 'QNX',
            'BeOS' => 'BeOS',
            'OS2' => 'OS/2',
            'SearchBot' => '(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
        );
        $uagent = strtolower($uagent ? $uagent : $_SERVER['HTTP_USER_AGENT']);
        foreach ($oses as $os => $pattern)
            if (preg_match('/' . $pattern . '/i', $uagent))
                return $os;
        return 'Unknown';
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */



